<?php
/**
 * Created by IntelliJ IDEA.
 * User: wanglu
 * Date: 2017/7/4
 * Time: 15:49
 */
/**
 * Created by wanglu on 2017/7/4.
 */

class TimeUtil
{

    const TIME_ZONE_UTC = 1;
    const TIME_ZONE_GMT8 = 2;


    public static $timezones = array(
        self::TIME_ZONE_UTC => 'UTC',
        self::TIME_ZONE_GMT8 => 'GMT+8'
    );

    public static function getTimeZoneName($timezone)
    {
        return self::$timezones[$timezone];
    }

    public static function convertUTC2GMTTimestamp($timestamp) {
       return  mdate("%Y-%m-%d  %H:%i:%s", TimeUtil::convertTimeZone("GMT+8", $timestamp));

    }

    public  static function showtime($timestamp, $timzone, $datetime = true) {
        $convertTimestamp = 0;
        switch ($timzone) {
            case self::TIME_ZONE_UTC:
                $convertTimestamp = $timestamp;
                break;
            case self::TIME_ZONE_GMT8:

                $convertTimestamp = TimeUtil::convertTimeZone(self::$timezones[$timzone], $timestamp);
                break;
        }

        if ($datetime) {
            return mdate("%Y-%m-%d  %H:%i:%s", $convertTimestamp);
        }

        return mdate("%Y-%m-%d", $convertTimestamp);
    }


    /**
     *
     * 转成UTC时间
     * @param $timestamp
     * @param $current_timezone
     * @return int|mixed|number
     */

    public static function convert2UTCTime($timestamp,  $current_timezone) {
        return TimeUtil::convertTimeZone(self::$timezones[self::TIME_ZONE_UTC], $timestamp, TimeUtil::getTimeZoneName($current_timezone));
    }


    public static function convertTimeZone($target_timezone, $timestamp, $current_timezone = 'UTC')
    {
        if (empty($timestamp))
        {
            $timestamp = time();
        }

        if (strpos($target_timezone, 'GMT') !== false || strpos($target_timezone, 'UTC') !== false)
        {

            $offsets = str_replace('GMT', '', $target_timezone); // GMT+08:00 -> +08:00
            $offsets = str_replace('UTC', '', $offsets); // UTC+08:00 -> +08:00


            if (!empty($offsets))
            {
                if (strpos($offsets, ':') !== false) // +08:00 -> 08.00 or -08:00 -> -08.00
                {
                    $hour = str_replace(':', '.', $offsets);
                }
                else
                {
                    $hour = $offsets;
                }

                $second = $hour * 3600;

                if ($second > 0)
                {
                    $timestamp += abs($second);
                }
                else
                {
                    $timestamp += $second;
                }
            }
        }
        else
        {
            $date = date_create(date("Y-m-d H:i:s", $timestamp), timezone_open($target_timezone));
            $timestamp = date_timestamp_get($date);

        }


        if (strpos($current_timezone, 'GMT') !== false || strpos($current_timezone, 'UTC') !== false)
        {


            $offsets = str_replace('GMT', '', $current_timezone); // GMT+08:00 -> +08:00
            $offsets = str_replace('UTC', '', $offsets); // UTC+08:00 -> +08:00
            
            if (!empty($offsets))
            {
                if (strpos($offsets, ':') !== false) // +08:00 -> 08.00 or -08:00 -> -08.00
                {
                    $hour = str_replace(':', '.', $offsets);
                }
                else
                {
                    $hour = $offsets;
                }
                $second = $hour * 3600;

                if ($second > 0)
                {
                    $timestamp -= abs($second);
                }
                else
                {
                    $timestamp += abs($second);
                }
            }
        }
        else
        {
            $date = date_create(date("Y-m-d H:i:s", $timestamp), timezone_open($current_timezone));
            $timestamp = date_timestamp_get($date);
        }

        return $timestamp;
    }
}